package com.atguigu.gmall.search.listener;


import com.atguigu.core.bean.Resp;
import com.atguigu.gmall.pms.entity.*;
import com.atguigu.gmall.search.feign.GmallPmsFeign;
import com.atguigu.gmall.search.feign.GmallWmsFeign;
import com.atguigu.gmall.search.pojo.Goods;
import com.atguigu.gmall.search.pojo.SearchAttr;
import com.atguigu.gmall.search.repository.GoodsRepository;
import com.atguigu.gmall.wms.entity.WareSkuEntity;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Component //注册进spring容器
public class GoodsListener {

    @Autowired
    private GmallPmsFeign gmallPmsFeign;

    @Autowired
    private GmallWmsFeign gmallWmsFeign;

    @Autowired
    private GoodsRepository goodsRepository;
    
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "gmall-search-queue",durable = "true"),
            exchange = @Exchange(value = "GAMLL-PMS-EXCHANGE",type = ExchangeTypes.TOPIC,ignoreDeclarationExceptions = "true"),
            key={"item.insert"}
    ))
    public void listener(Long spuId){
        //根据遍历得到的spuid查询对应的sku分页信息
        Resp<List<SkuInfoEntity>> skuResp = this.gmallPmsFeign.querySkuBySpuId(spuId);
        List<SkuInfoEntity> skus = skuResp.getData();//得到所有的sku

        //判断sku是否为空
        if(!CollectionUtils.isEmpty(skus)){
            //如果不为空，执行保存操作

            //把skus转换为goods对象
            List<Goods> goodsList=skus.stream().map(skuInfoEntity -> {
                Goods goods=new Goods();

                //查询属性及值
                Resp<List<ProductAttrValueEntity>> attrValueResp = this.gmallPmsFeign.querySearchAttrValueBySpuId(spuId);
                List<ProductAttrValueEntity> attrValueEntities = attrValueResp.getData();
                if(!CollectionUtils.isEmpty(attrValueEntities)){
                    //不为空，才能设置值
                    List<SearchAttr> searchAttrs=attrValueEntities.stream().map(
                            productAttrValueEntity -> {
                                SearchAttr searchAttr=new SearchAttr();
                                searchAttr.setAttrId(productAttrValueEntity.getAttrId());
                                searchAttr.setAttrName(productAttrValueEntity.getAttrName());
                                searchAttr.setAttrValue(productAttrValueEntity.getAttrValue());
                                return searchAttr;
                            }
                    ).collect(Collectors.toList());
                    goods.setAttrValueList(searchAttrs);

                }

                //查询品牌并设置值
                Resp<BrandEntity> brandEntityResp = this.gmallPmsFeign.queryBrandById(skuInfoEntity.getBrandId());
                BrandEntity brandEntity = brandEntityResp.getData();
                if(brandEntity!=null){

                    goods.setBrandName(brandEntity.getName());
                }
                goods.setBrandId(skuInfoEntity.getBrandId());
                //查询分类信息并设置值
                Resp<CategoryEntity> categoryEntityResp = this.gmallPmsFeign.queryCategoryById(skuInfoEntity.getCatalogId());
                CategoryEntity categoryEntity = categoryEntityResp.getData();
                if(categoryEntity!=null){

                    goods.setCategoryName(categoryEntity.getName());
                }

                Resp<SpuInfoEntity> spuInfoEntityResp = this.gmallPmsFeign.querySpuById(spuId);
                SpuInfoEntity spuInfoEntity = spuInfoEntityResp.getData();
                goods.setCategoryId(skuInfoEntity.getCatalogId());
                goods.setCreateTime(spuInfoEntity.getCreateTime());//设置spu时间
                goods.setPic(skuInfoEntity.getSkuDefaultImg());

                goods.setPrice(skuInfoEntity.getPrice().doubleValue());

                //查询库存信息并且设置值
                Resp<List<WareSkuEntity>> listResp = this.gmallWmsFeign.queryWareSkuBySkuId(skuInfoEntity.getSkuId());
                List<WareSkuEntity> wareSkuEntities = listResp.getData();
                if(!CollectionUtils.isEmpty(wareSkuEntities)){
                    boolean flag=wareSkuEntities.stream().anyMatch(wareSkuEntity ->
                                    wareSkuEntity.getStock()>0
                            //只要任何一个仓库里面有库存
                    );

                    goods.setStore(flag);
                }
                goods.setSale(0l);
                goods.setSkuId(skuInfoEntity.getSkuId());

                goods.setTitle(skuInfoEntity.getSkuTitle());
                return goods;
            }).collect(Collectors.toList());

            this.goodsRepository.saveAll(goodsList);
        }
    }
}
